package com.hellogood.yaohuo.client;

import com.hellogood.yaohuo.client.basic.ColorTab;
import com.hellogood.yaohuo.client.basic.CupTab;
import com.hellogood.yaohuo.client.basic.SizeTab;
import com.hellogood.yaohuo.client.bussiness.fanchang.FanchangTab;
import com.hellogood.yaohuo.client.bussiness.peisong.PeisongTab;
import com.hellogood.yaohuo.client.bussiness.tuihuo.TuihuoTab;
import com.hellogood.yaohuo.client.bussiness.yanshou.YanshouTab;
import com.hellogood.yaohuo.client.bussiness.yaohuo.YaohuoTab;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.client.common.Constances;
import com.hellogood.yaohuo.client.setting.GroupQxTab;
import com.hellogood.yaohuo.client.setting.GroupTab;
import com.hellogood.yaohuo.client.setting.QuyuTab;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.CloseClickHandler;
import com.smartgwt.client.widgets.tab.events.TabCloseClickEvent;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class MainPanel extends HLayout {
	private TreeNode[] functionData = null;

	private TreeGrid treeGrid = null;
	private TabSet tabSet = null;

	private ColorTab colorTab = null;
	private CupTab cupTab = null;
	private SizeTab sizeTab = null;

	private QuyuTab quyuTab = null;
	private GroupTab groupTab = null;
	private GroupQxTab groupQxTab = null;

	private YaohuoTab yaohuoTab = null;
	private YanshouTab yanshouTab = null;
	private PeisongTab peisongTab = null;
	private FanchangTab fanchangTab = null;
	private TuihuoTab tuihuoTab = null;

	public MainPanel() {
		initData();
		initView();
	}

	private void initData() {
		functionData = new TreeNode[] { new FunctionTreeNode("1", "-1", "系统设置"), new FunctionTreeNode("2", "-1", "基本资料"), new FunctionTreeNode("3", "-1", "业务操作"),
				new FunctionTreeNode("4", "-1", "报表查询"), new FunctionTreeNode("11", "1", "用户组设置"), new FunctionTreeNode("12", "1", "用户组权限"), new FunctionTreeNode("13", "1", "区域设置"),
				new FunctionTreeNode("21", "2", "颜色设置"), new FunctionTreeNode("22", "2", "尺寸设置"), new FunctionTreeNode("23", "2", "杯级设置"), new FunctionTreeNode("31", "3", "门店要货"),
				new FunctionTreeNode("32", "3", "区域汇总"), new FunctionTreeNode("33", "3", "采购汇总"), new FunctionTreeNode("34", "3", "总部汇总"), new FunctionTreeNode("35", "3", "区域汇总查询"),
				new FunctionTreeNode("36", "3", "采购汇总查询"), new FunctionTreeNode("37", "3", "总部汇总查询"), new FunctionTreeNode("38", "3", "配送"), new FunctionTreeNode("39", "3", "物流返厂"),
				new FunctionTreeNode("3a", "3", "物流验收"), new FunctionTreeNode("3b", "3", "移库"), new FunctionTreeNode("41", "4", "库存查询"), new FunctionTreeNode("42", "4", "台账查询"),
				new FunctionTreeNode("43", "4", "盘点"), new FunctionTreeNode("5", "-1", "操作日志") };
	}

	private void initView() {
		Tree menuTree = new Tree();
		menuTree.setModelType(TreeModelType.PARENT);
		menuTree.setNameProperty("name");
		menuTree.setRootValue("-1");
		menuTree.setIdField("nodeId");
		menuTree.setParentIdField("parentId");
		menuTree.setOpenProperty("isOpen");
		if (functionData != null) {
			for (TreeNode node : functionData) {
				node.setAttribute("isOpen", true);
			}
		}
		menuTree.setData(functionData);

		treeGrid = new TreeGrid();
		treeGrid.setFields(new TreeGridField("name", "操作"));
		treeGrid.setData(menuTree);
		treeGrid.setHeight("100%");
		treeGrid.addDoubleClickHandler(new DoubleClickHandler() {

			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				Record r = treeGrid.getSelectedRecord();
				if (r != null) {
					String nodeID = r.getAttribute("nodeId");
					clickTreeNode(nodeID);
				}
			}
		});

		VLayout leftSideLayout = new VLayout();
		leftSideLayout.addMember(treeGrid);
		leftSideLayout.setWidth("200");
		leftSideLayout.setHeight("100%");
		addMember(leftSideLayout);

		tabSet = new TabSet();
		tabSet.setSize("100%", "100%");
		tabSet.addCloseClickHandler(new CloseClickHandler() {

			@Override
			public void onCloseClick(TabCloseClickEvent event) {
				event.cancel(); // MUST DO THIS FIRST
				SC.confirm(CommonMessages.ConfirmClose, new ConfirmBooleanCallback(event));
			}
		});
		VLayout rightSideLayout = new VLayout();
		rightSideLayout.setWidth("85%");
		rightSideLayout.setHeight("100%");
		rightSideLayout.addMember(tabSet);
		addMember(rightSideLayout);

		setMargin(10);
		setWidth100();
		setHeight100();
	}

	private class FunctionTreeNode extends TreeNode {
		public FunctionTreeNode(String nodeID, String parentID, String name) {
			setAttribute("nodeId", nodeID);
			setAttribute("parentId", parentID);
			setAttribute("name", name);
		}
	}

	private void clickTreeNode(String nodeID) {
		if (nodeID.equals("21")) {
			if (colorTab != null && tabSet.getTab(colorTab.getID()) != null) {
				tabSet.selectTab(colorTab);
			} else {
				colorTab = new ColorTab();
				colorTab.setTitle("颜色设置");
				tabSet.addTab(colorTab);
				tabSet.selectTab(colorTab);
			}
		} else if (nodeID.equals(Constances.TREE_ID_OF_CUP_SETTING)) {
			if (cupTab != null && tabSet.getTab(cupTab.getID()) != null) {
				tabSet.selectTab(cupTab);
			} else {
				cupTab = new CupTab();
				cupTab.setTitle("杯级设置");
				tabSet.addTab(cupTab);
				tabSet.selectTab(cupTab);
			}

		} else if (nodeID.equals(Constances.TREE_ID_OF_SIZE_SETTING)) {
			if (sizeTab != null && tabSet.getTab(sizeTab.getID()) != null) {
				tabSet.selectTab(sizeTab);
			} else {
				sizeTab = new SizeTab();
				sizeTab.setTitle("尺寸设置");
				tabSet.addTab(sizeTab);
				tabSet.selectTab(sizeTab);
			}
		} else if (nodeID.equals(Constances.TREE_ID_OF_QUYU_SETTING)) {
			if (quyuTab != null && tabSet.getTab(quyuTab.getID()) != null) {
				tabSet.selectTab(quyuTab);
			} else {
				quyuTab = new QuyuTab();
				quyuTab.setTitle("区域设置");
				tabSet.addTab(quyuTab);
				tabSet.selectTab(quyuTab);
			}
		} else if (nodeID.equals(Constances.TREE_ID_OF_GROUP_SETTING)) {
			if (groupTab != null && tabSet.getTab(groupTab.getID()) != null) {
				tabSet.selectTab(groupTab);
			} else {
				groupTab = new GroupTab();
				groupTab.setTitle("用户组设置");
				tabSet.addTab(groupTab);
				tabSet.selectTab(groupTab);
			}
		} else if (nodeID.equals(Constances.TREE_ID_OF_GROUP_AUTHORITY)) {
			if (groupQxTab != null && tabSet.getTab(groupQxTab.getID()) != null) {
				tabSet.selectTab(groupQxTab);
			} else {
				groupQxTab = new GroupQxTab();
				groupQxTab.setTitle("用户组权限");
				tabSet.addTab(groupQxTab);
				tabSet.selectTab(groupQxTab);

			}
		} else if (nodeID.equals(Constances.TREE_ID_OF_SHOP_YAOHUO)) {
			if (yaohuoTab != null && tabSet.getTab(yaohuoTab.getID()) != null) {
				tabSet.selectTab(yaohuoTab);
			} else {
				yaohuoTab = null;
				yaohuoTab = new YaohuoTab();
				yaohuoTab.setTitle("门店要货");
				tabSet.addTab(yaohuoTab);
				tabSet.selectTab(yaohuoTab);
			}
		} else if (nodeID.equals(Constances.TREE_ID_OF_WULIU_YANSHOU)) {
			if (yanshouTab != null && tabSet.getTab(yanshouTab.getID()) != null) {
				tabSet.selectTab(yanshouTab);
			} else {
				yanshouTab = null;
				yanshouTab = new YanshouTab();
				yanshouTab.setTitle("物流验收");
				tabSet.addTab(yanshouTab);
				tabSet.selectTab(yanshouTab);
			}
		} else if (nodeID.equals(Constances.TREE_ID_OF_PEISONG)) {
			if (peisongTab != null && tabSet.getTab(peisongTab.getID()) != null) {
				tabSet.selectTab(peisongTab);
			} else {
				peisongTab = null;
				peisongTab = new PeisongTab();
				peisongTab.setTitle("商品配送");
				tabSet.addTab(peisongTab);
				tabSet.selectTab(peisongTab);
			}
		} else if (nodeID.equals(Constances.TREE_ID_OF_WULIU_FANCHANG)) {
			if (fanchangTab != null && tabSet.getTab(fanchangTab.getID()) != null) {
				tabSet.selectTab(fanchangTab);
			} else {
				fanchangTab = null;
				fanchangTab = new FanchangTab();
				fanchangTab.setTitle("退货返厂");
				tabSet.addTab(fanchangTab);
				tabSet.selectTab(fanchangTab);
			}
		} else if (nodeID.equals(Constances.TREE_ID_OF_YIKU)) {
			if (tuihuoTab != null && tabSet.getTab(tuihuoTab.getID()) != null) {
				tabSet.selectTab(tuihuoTab);
			} else {
				tuihuoTab = null;
				tuihuoTab = new TuihuoTab();
				tuihuoTab.setTitle("配送退货");
				tabSet.addTab(tuihuoTab);
				tabSet.selectTab(tuihuoTab);
			}
		}
	}

	private class ConfirmBooleanCallback implements BooleanCallback {
		private TabCloseClickEvent event;

		public ConfirmBooleanCallback(TabCloseClickEvent event) {
			this.event = event;
		}

		@Override
		public void execute(Boolean value) {
			if (value != null && value) {
				Tab tab = event.getTab();
				tabSet.removeTab(tab);
				tab = null;
			}
		}
	}
}
